const {
    http
} = require("../../utils/apiutils")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: "",
        id: "",
    },

    //登录系统
    loginsys() {
        var data = {
            "phone": "17319743876", //手机号码
            "password": "a123456" //密码
        }
        var that = this;
        wx.showLoading({
            title: '加载中...',
        })
        http.authenticate(data,
            function (res) {
                wx.hideLoading()
                console.log("------用户登录信息哦---", res);
                if (res.code == 0) {
                    getApp().globalData.token = res.token;
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: "none",
                    })
                }
            },
            function (err) {
                wx.hideLoading()
            })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    //获取用户信息
    getuser: function () {
        var that = this;
        wx.showLoading({
            title: '加载中...',
        })
        http.userInfo({},
            function (res) {
                wx.hideLoading()
                console.log("------用户信息--", res);
                if (res.code == 0) {
                    getApp().globalData.userInfo = res.data;
                    console.log('userInfo', getApp().globalData)
                    console.log('判断route')
                    if (getApp().globalData.route) {
                        console.log('进入route')
                        let url = ""
                        switch (getApp().globalData.route) {
                            case 'searchgoods':
                                url = '/pages/clock/pages/searchgoods/searchcar'
                                break;
                            case 'searchcar':
                                url = '/pages/clock/pages/searchcar/searchcar'
                                break;
                            case 'searchGDtl':
                                url = '/pages/clock/pages/searchGDtl/index?' + getApp().globalData.routeParams
                                break;
                            case 'activity':
                                url = '/pages/home/pages/activity/activity?' + getApp().globalData.routeParams
                                break;
                            case 'information':
                                url = '/pages/tabBar/information/index'
                                break;
                            case 'list-dtl':
                                url = '/pages/information/pages/list-dtl/index?'+getApp().globalData.routeParams
                                break;
                            case 'work':
                                url = '/pages/home/pages/work/work?'+getApp().globalData.routeParams
                                break;
                            case 'renwuwork':
                                url = '/pages/home/pages/renwuwork/work?'+getApp().globalData.routeParams
                                break;
                            case 'jobvideo':
                                url = '/pages/clock/pages/jobvideo/index?'+getApp().globalData.routeParams
                                break;
                        }
                        wx.redirectTo({
                            url: url,
                            success() {
                                getApp().globalData.route = ""
                            },
                            fail(err) {
                                console.log(err)
                            }
                        })
                    } else if (that.data.type == "login") {
                        wx.switchTab({
                            url: '/pages/tabBar/home/index',
                        })
                    } else if (that.data.type == "work") {
                        wx.redirectTo({
                            url: '/pages/home/pages/work/work?id=' + that.data.id,
                        })
                    } else if (that.data.type == "zzzp") {
                        wx.redirectTo({
                            url: '/pages/home/pages/recruit/recruit',
                        })
                    } else {
                        console.log('揍else')
                        wx.switchTab({
                            url: '/pages/tabBar/home/index',
                        })
                        // wx.navigateTo({
                        //   url: '/pages/me/pages/action/action',
                        // })
                    }
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: "none",
                    })
                }
            },
            function (err) {
                wx.hideLoading()
            })

    },
    wclogin: function () {
        var that = this;
        wx.login({ //调用获取用户openId
            success: function (res) {
                that.wxlogin(res.code)
            }
        })
    },
    wxlogin: function (code) {
        var that = this;
        wx.showLoading({
            title: '加载中...',
        })
        http.weChatLogin({
                code: code,
                userFrom:1
            },
            function (res) {
                wx.hideLoading()
                console.log("------用户信息--", res);
                if (res.code == 0) {
                    getApp().globalData.isLogin = true;
                    getApp().globalData.token = res.token;
                    getApp().globalData.unionid = res.unionid;
                    getApp().globalData.openid = res.openid;
                    console.log('weChatLogin', getApp().globalData)
                    that.getuser();
                } else if (res.code == 1 || res.code == 2) {
                    getApp().globalData.openid = res.openid;
                    getApp().globalData.unionid = res.unionid;
                    getApp().globalData.isLogin = false;
                    if (getApp().globalData.route) {
                        wx.redirectTo({
                            url: '/pages/login/login',
                        })
                    } else if (that.data.type == "login") {
                        wx.switchTab({
                            url: '/pages/tabBar/home/index',
                        })
                    } else if (that.data.type == "zzzp") {
                        wx.reLaunch({
                            url: '/pages/home/pages/recruit/recruit',
                        })
                    } else if (that.data.type == "work" && that.data.id) {
                        wx.navigateTo({
                            url: '/pages/login/login?type=1&ptype=' + that.data.type + "&id=" + that.data.id,
                            success: (result) => {},
                            fail: (res) => {},
                            complete: (res) => {},
                        })
                    } else {
                        wx.switchTab({
                            url: '/pages/tabBar/home/index',
                        })

                    }

                } else {
                    wx.showToast({
                        title: res.message,
                        icon: "none",
                    })
                }
            },
            function (err) {
                wx.hideLoading()
            })
    },
    onLoad: function (options) {

        // this.loginsys()
        // this.getuser();
        console.log("--------获取进入时的数据---", options);
        if (options.type) {
            this.setData({
                type: options.type,
            })
            if (options.type == "work") {
                getApp().globalData.inviteCode = options.code;
                this.setData({
                    id: options.id
                })
            }
        }
        if (options.code) {
            getApp().globalData.inviteCode = options.code;
        }
        console.log('判断route');
        if (options.route) {
            console.log('进入有route');
            if (options.route == 'searchGDtl') {
                getApp().globalData.route = options.route;
                getApp().globalData.routeParams = 'id=' + options.dtlId;
                getApp().globalData.inviteCode = options.code;
            }
            if (options.route == 'searchcar') {
                getApp().globalData.route = options.route;
                getApp().globalData.inviteCode = options.code;
            }
            if (options.route == 'searchgoods') {
                getApp().globalData.route = options.route
            }
            if (options.route == 'information') {
                getApp().globalData.route = options.route
            }
            if (options.route == 'list-dtl') {
                getApp().globalData.route = options.route;
                getApp().globalData.inviteCode = options.code;
                getApp().globalData.routeParams = 'item=' + options.item;
            }
            if (options.route == 'work') {
                getApp().globalData.route = options.route;
                getApp().globalData.inviteCode = options.code;
                getApp().globalData.routeParams ='id=' + options.id+'&code='+options.code;
            }
            if (options.route == 'renwuwork') {
                getApp().globalData.route = options.route;
                getApp().globalData.inviteCode = options.code;
                getApp().globalData.routeParams = 'id=' + options.id;
            }
            if (options.route == 'jobvideo') {
                getApp().globalData.route = options.route;
                getApp().globalData.routeParams = 'id=' + options.id;
            }
        }
        //  扫码进入
        if (options.scene) {
            console.log('扫码进入进入时携带的参数', options)
            if (this.getScene(options.scene).event) {
                // 是否活动
                getApp().globalData.isActivity = this.getScene(options.scene).event;
            }
            if (this.getScene(options.scene).route == 'activity') {
                // 福利活动跳转
                getApp().globalData.route = this.getScene(options.scene).route;
                getApp().globalData.routeParams = 'id=' + this.getScene(options.scene).id
            }
            if (this.getScene(options.scene).route == 'searchgoods') {
                // 公益列表跳转
                getApp().globalData.route = this.getScene(options.scene).route;
            }
            if (this.getScene(options.scene).route  == 'work') {
                getApp().globalData.route = this.getScene(options.scene).route;
                getApp().globalData.routeParams = 'id=' + this.getScene(options.scene).id;
            }
            if (options.route == 'renwuwork') {
                getApp().globalData.route = options.route;
                getApp().globalData.routeParams = 'id=' + options.id;
            }
            if (options.route == 'jobvideo') {
                getApp().globalData.route = options.route;
                getApp().globalData.routeParams = 'id=' + options.id;
            }
            console.log('扫码携带的参数', this.getScene(options.scene))
        }
        wx.getSystemInfo({
            success: (e) => {
                // console.log("-----状态栏高度------", e.statusBarHeight);
                let capsule = wx.getMenuButtonBoundingClientRect();
                console.log("-----------", capsule);
                if (capsule) {
                    getApp().globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
                } else {
                    getApp().globalData.CustomBar = e.statusBarHeight + 50;
                }
                getApp().globalData.statusBar = e.statusBarHeight;
                // console.log("-----------", getApp().globalData.CustomBar);
            },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.wclogin()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareObj = {
            title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
            path: getApp().globalData.userInfo.inviteCode ? '/pages/index/index?code=' + getApp().globalData.userInfo.inviteCode : '/pages/index/index', // 
            imageUrl: '',
            success: function (res) {
                // 转发成功之后的回调
                if (res.errMsg == 'shareAppMessage:ok') {}
            }
        }
        console.log("------------", shareObj);
        // 返回shareObj
        return shareObj;
    },
    getScene(scene = "") {
        console.log('扫描出的参数', scene)
        if (scene == "") return null;
        let res = {};
        let res1 = {};
        let params = decodeURIComponent(scene).split("&");
        console.log('新的params', params)
        params.forEach(item => {
            let pram = item.split("=")
            res[pram[0]] = pram[1]
        })
        console.log('格式化后的参数', res)
        return res
    }
})